package com.haoqizhe.kernel.activemq.listener;

import com.haoqizhe.kernel.activemq.connection.ActiveMQConnectionFactoryConfiguration;
import org.apache.activemq.ActiveMQSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;

import javax.jms.Session;

/**
 * @author  haoqizhe.li on 2017/11/3.
 */
@Configuration
@DependsOn(value = "cachingReceiveConnectionFactory")
@Import({ActiveMQConnectionFactoryConfiguration.class,ListenerHandler.class})
public class QueueListenerContainerConfig {

    @Autowired
    @Qualifier(value = "cachingReceiveConnectionFactory")
    private CachingConnectionFactory cachingReceiveConnectionFactory;
    /**
     * 配置queue监听器,事务提交确认
     * @return JmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/12 21:04
     **/
    @Bean(name="containerFactory4QueueSession")
    public DefaultJmsListenerContainerFactory containerFactory4QueueSession(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(cachingReceiveConnectionFactory);

        /**
         * 开启事务支持，为true时，sessionAcknowledgeMode 无效，默认为 Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionTransacted(true);
        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED);

        factory.setConcurrency("3-10");

        return factory;
    }

    /**
     * 配置queue监听器，自动确认
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:16
     **/
    @Bean(name="containerFactory4QueueAuto")
    public DefaultJmsListenerContainerFactory containerFactory4QueueAuto(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(cachingReceiveConnectionFactory);

        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

        factory.setConcurrency("3-10");

        return factory;
    }
    /**
     * 配置queue监听，客户端手动确认
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:17
     **/
    @Bean(name="containerFactory4QueueIndividual")
    public DefaultJmsListenerContainerFactory containerFactory4QueueClient(){

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);

        /**
         * 开启事务支持，为true时，sessionAcknowledgeMode 无效，默认为 Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionTransacted(false);
        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         * ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE : 4  客户端确认，每条显示调用acknowledge()
         *
         */
        factory.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);

        factory.setConcurrency("3-10");

        return factory;
    }
    /**
     * 配置queue监听器，自动批量确认
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:18
     **/
    @Bean(name="containerFactory4QueueDups")
    public DefaultJmsListenerContainerFactory containerFactory4QueueDups(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(cachingReceiveConnectionFactory);

        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         * ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE : 4  客户端确认，每条显示调用acknowledge()
         *
         */
        factory.setSessionAcknowledgeMode(Session.DUPS_OK_ACKNOWLEDGE);

        factory.setConcurrency("3-10");

        return factory;
    }

    /**
     * 配置事务监听，默认单线程
     * @return
     */
    @Bean(name="containerFactory4QueueSessionOne")
    public DefaultJmsListenerContainerFactory containerFactory4QueueSessionOne(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(cachingReceiveConnectionFactory);

        /**
         * 开启事务支持，为true时，sessionAcknowledgeMode 无效，默认为 Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionTransacted(true);
        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED);

        return factory;
    }
}
